package com.ihunanren.biz.weixin.ao.impl;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.isNullOrEmpty;

import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.util.UUID;
import com.ihunanren.biz.result.Result;
import com.ihunanren.biz.result.ResultSupport;
import com.ihunanren.biz.weixin.ao.IAppUserAO;
import com.ihunanren.biz.weixin.domain.Register;
import com.ihunanren.dao.pojo.AppUserDO;
import com.ihunanren.dao.weixin.IAppUserDao;

/**
 * @author kalman03
 */
public class AppUserAO implements IAppUserAO {

	private final static Logger log = LoggerFactory.getLogger(AppUserAO.class);

	private final static String email_regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	private final static String phone_regex = "";

	@Autowired
	private IAppUserDao appUserDao;

	@Override
	public Result generateUserToken(long userId) {
		Result result = new ResultSupport(false);
		try {
			String token = new UUID(true).nextID();
			boolean success = appUserDao.updateTokenById(userId, token);
			if (success) {
				result.setSuccess(true);
				result.setDefaultModel(token);
			}
		} catch (Exception e) {
			log.error("generateUserToken catch a exception.", e);
		}
		return result;
	}

	@Override
	public Result isUserMatched(String key, String password) {
		checkArgument(!isNullOrEmpty(key) && !isNullOrEmpty(password),"userName and password is required");
		Result result = new ResultSupport(false);
		try {
			AppUserDO appUserDO = null;
			if (Pattern.compile(email_regex).matcher(key).find()) {
				appUserDO = appUserDao.getAppUserByEmail(key);
			} else if (Pattern.compile(phone_regex).matcher(key).find()) {
				appUserDO = appUserDao.getAppUserByPhone(key);
			} else {
				appUserDO = appUserDao.getAppUserByNick(key);
			}
			//FIXME password需要加密对比
			if (appUserDO != null && appUserDO.getPassword().equals(password)) {
				result.setSuccess(true);
				result.setDefaultModel(appUserDO);
			}
		} catch (Exception e) {
			log.error("isUserMatched catch a exception.", e);
		}
		return result;
	}

	@Override
	public Result register(Register register) {
		Result result = new ResultSupport(false);
		try {
			// FIXME check register
			AppUserDO appUserDO = createAppUserDO(register);
			boolean success = appUserDao.insertAppUser(appUserDO);
			if (success) {
				result.setSuccess(true);
				result.setDefaultModel(true);
			}
		} catch (Exception e) {
			log.error("register catch a exception.", e);
		}
		return result;
	}

	private AppUserDO createAppUserDO(Register register) {
		AppUserDO appUserDO = new AppUserDO();
		appUserDO.setEmail(register.getEmail());
		appUserDO.setNick(register.getNick());
		appUserDO.setPassword(register.getPassword1());// FIXME 加密处理
		appUserDO.setPhone(register.getPhone());
		appUserDO.setRemarks(register.getRemarks());
		String token = new UUID(true).nextID();
		appUserDO.setWeixinToken(token);
		return appUserDO;
	}
}
